/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.brainstretch.jsf;

import java.io.IOException;
import java.io.Serializable;
import java.security.Principal;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import org.brainstretch.ejb.managers.UserSessionManager;
import org.brainstretch.jsf.util.JsfUtil;
import org.brainstretch.models.User;

/**
 *
 * @author mrodaitis
 */
@ManagedBean
@SessionScoped
public class UserController implements Serializable{

    /**
     * Creates a new instance of UserController
     */
    public UserController() {
    }
    
    @EJB
    UserSessionManager userManager;
    
    private User user;
    
    private String username;
    private String password;
    
    public String login(){
        HttpServletRequest request = (HttpServletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest();
        String result = null;               
        try {
            request.login(username, password);            
            user = userManager.findUserByName(username);
            username = password = null;
            //TODO redirect to the original request URL if it exists
            FacesContext.getCurrentInstance().getExternalContext().redirect(JsfUtil.getRequest().getContextPath()+"/main/home.xhtml");
            
        } catch (ServletException ex) {
           // ex.printStackTrace(System.err);
            JsfUtil.addErrorMessage("Incorrect username or password");
        }        
        catch(IOException ex){
            JsfUtil.addErrorMessage("Unable to redirect after login.");
        }
        return result;
    }
    
    public boolean isUserLoggedIn(){
        Principal userPrincipal =  FacesContext.getCurrentInstance().getExternalContext().getUserPrincipal();
        if(userPrincipal != null && user == null){
            user = userManager.findUserByName(userPrincipal.getName());
        }
        return user != null;
    }
    
    public String logout(){
        user = null;
        FacesContext.getCurrentInstance().getExternalContext().invalidateSession();
        HttpServletRequest request = (HttpServletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest();
        if(request.getUserPrincipal() != null){
            try {
                request.logout();
            } catch (ServletException ex) {
                JsfUtil.addErrorMessage("Logout failed. YOU CAN'T LEAVE!");
                ex.printStackTrace(System.err);
            }
        }
        
        return "home";
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }
        
}
